<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>出入库信息管理</title>
	<meta name="decorator" content="default"/>
	<style type="text/css">
		.w250{
			width: 250px !important;
		}
	</style>
	<script type="text/javascript">
		$(document).ready(function(){
			init();
			initTable();
		});
		var areaData;
		function init(){
			debugger
				 var url="${pageContext.request.contextPath}/a/area/areaChoose/getArea";
					$.ajax({ 
						url: url,
						type: "GET",
						dataType:"json",
						cache: false,
						async:false,
						success: function(data){
							debugger
							areaData=data;
				        },
				        error:function(){
				        	return ;
				        }
					});
				
		}
		
		function initTable(id){
			var pid = getParentId(id);
			var pname = getParentName(id);
			var data =[];
			if(areaData.length==0){
				return ;
			}else{
				if(id){
					$(".area").empty();
					$(".area").append('<li><a href="javascript:history.go(-1)">返回上一级</a></li>');
					if(pid&&pname){
						$(".area").append('<li><a href="#" onclick="initTable('+pid+')">'+pname+'</a></li>');
						for(var i in areaData){
							if(areaData[i].parentId == pid){
								data.push(areaData[i]);
							}
						}
					}else{
						data.push(areaData[0]);
					}
				}else{
					return getAreaByFatherCode(${officeId},this);
				}
			}
			
			if(data.length == 0){
				return ;
			}
			var buffer = new StringBuffer();
			for(var i=0;i<data.length;i++){
				buffer.append('<div class="control-group "> ');
				buffer.append('<a class="control-label w250" onclick="getAreaByFatherCode('+data[i].id+',this)" style="cursor: pointer;">'+data[i].name+'</a>&nbsp;&nbsp;<input  class="btn btn-primary " onclick="choose(this,'+data[i].code+')" type="button" value="选择">');
				buffer.append('</div>');
			}
			$(".content").empty();
			$(".content").append(buffer.toString());
		}
		function choose(obj,code){
			debugger
		 	var str=$(obj).parent().find("a").text();
		 	parent.layer.closeAll();
		 	parent.setReceive(str,code);
	}
		//根据父节点获得所属子节点
		function  getAreaByFatherCode(id,obj){
			var data = [];
			if(areaData.length == 0){
				return;
			}else{
				for(var i in areaData){
					if(areaData[i].parentId == id)
					data.push(areaData[i]);
				}
			}
			if(data.length == 0){
				return ;
			}else{
				var name = getName(id);
				$(".area").empty();
				$(".area").append('<li><a href="#" )>返回上一级</a></li>');
				$(".area").append('<li><a href="#"  onclick="initTable('+id+',this)">'+name+'</a></li>');
			}
			var buffer = new StringBuffer();
			for(var i=0;i<data.length;i++){
				buffer.append('<div class="control-group "> ');
				buffer.append('<a class="control-label w250" onclick="getAreaByFatherCode('+data[i].id+',this)" style="cursor: pointer;">'+data[i].name+'</a>&nbsp;&nbsp;<input  class="btn btn-primary" onclick="choose(this,'+data[i].code+')" type="button" value="选择">');
				buffer.append('</div>');
			}
			$(".content").empty();
			$(".content").append(buffer.toString());	
		}
		//定义
		function StringBuffer() {
		     this.__strings__ = new Array();
		 }
		 StringBuffer.prototype.append = function (str) {
		     this.__strings__.push(str);
		     return this;    //方便链式操作
		}
		StringBuffer.prototype.toString = function () {
		     return this.__strings__.join("");
		 }
		
		//获取父节点id
		function getParentId(id){
			for(var i in areaData){
				if(areaData[i].id == id){
					return areaData[i].parentId;
				}
			}
			return "";
		}
		//根据id获取当前节点名称
		function getName(id){
			for(var i in areaData){
				if(areaData[i].id == id){
					return  areaData[i].name;
				}
			}
			return "";
		}
		function getParentName(id){
			for(var i in areaData){
				if(areaData[i].id ==id){
					for(var j in areaData){
						if(areaData[j].id == areaData[i].parentId){
							return  areaData[j].name;
						}
					}
				}
			}
			return "";
		}
		}		
	</script>
	 
</head>
<body>
	<ul class="nav nav-tabs area">
	
	</ul><br/>
	<form:form  class="form-horizontal">
		<sys:message content="${message}"/>		
		<div class="content">
		
		</div>
	</form:form>
</body>
</html>